<?php
/**
 * Define an interface for building queries
 *
 * <p>A query builder should assist the mapping process by
 * inserting the column names and column values in the correct
 * places and with the correct formatting.
 * 
 * TODO most likely trying to introduce more than one table will
 * bring a slew of problems to this interface and its goals.
 * 
 * @package junction.query
 */
abstract class Junction_Query_Builder {
	
	/**
	 * Bind a table to the query
	 *
	 * @param String $table
	 */
	abstract public function bindTable($table);
	
	/**
	 * Bind a value to the primary key
	 *
	 * @param String $property
	 */
	abstract public function bindKey($column, $value);
	
	/**
	 * Bind a value to a non-key column
	 *
	 * @param String $column
	 * @param String $value
	 */
	abstract public function bindProperty($column, $value);
	
	/**
	 * Retrieve the query as a string
	 * 
	 * <p>Not all dao libraries (ADODB for example) can take the builder
	 * as an object and call __toString on it correctly so it's best to
	 * have an explicit method call instead.
	 *
	 * @return String
	 */
	abstract public function toSql();
	
	/**
	 * Escape the given data so it is safe to insert into the database
	 * 
	 * TODO make this database agnostic
	 * TODO move this to the Core_Mapping where it can leverage the DAO
	 *
	 * @param unknown_type $data
	 * @return unknown the escaped data to insert into the db
	 */
	protected function escape($data)
	{
		switch(gettype($data))
		{
			case 'string':
				$data = "'" . mysql_real_escape_string($data) . "'";
				break;
			case 'double':
				$data = str_replace(',', '.', $data);
				break;
			case 'boolean':
				$data = ($data) ? 1 : 0;
				break;
			default:
				if ($data == null)
					$data = "NULL";
		}
		return $data;
	}
}
?>